There’s no point in creating an array solely for the purpose of passing it as a varargs (...
) argument; varargs is an array.
Simply pass the elements directly. They will be consolidated into an array automatically. Incidentally passing an array where Object ...
is expected makes the intent ambiguous: Is the array supposed to be one object or a collection of objects?
Noncompliant code example
public void callTheThing() {
//...
doTheThing(new String[] { "s1", "s2"}); // Noncompliant: unnecessary
doTheThing(new String[12]); // Compliant
doTheOtherThing(new String[8]); // Noncompliant: ambiguous
// ...
}
public void doTheThing (String ... args) {
// ...
}
public void doTheOtherThing(Object ... args) {
// ...
}
Compliant solution
public void callTheThing() {
//...
doTheThing("s1", "s2");
doTheThing(new String[12]);
doTheOtherThing((Object[]) new String[8]);
// ...
}
public void doTheThing (String ... args) {
// ...
}
public void doTheOtherThing(Object ... args) {
// ...
}